// pages/Type/TypeDes/index.js
var common = require("../../../public.js");
Page({
    data: {
        isMore: true,
        storeList: [],
        pageNum: 1,
        pageSize: 10,
        cid: '',
        imageUrl: [],
        lat: '',
        lon: '',
        isLoadingBusy: false,
        refreshing: false,
        lun: true,
        industryType: '', //跳转模板
    },
    // 开始播放
    begain(e) {
        this.setData({
            lun: false
        })
    },
    // 播放完毕
    ended(e) {
        this.setData({
            lun: true
        })
    },
    // 获取上方的图片
    getTopImage() {
        var that = this
        common.get('/advertisements/storeListTopAds', { storeType: this.data.cid }, res => {
            if (res.status === 200) {
                this.setData({
                    imageUrl: res.data
                })
            } else {
                common.showRequestErrMsg(res)
            }
        })
    },
    // 判断模板 industryType   行业类型  1通用行业 2餐饮行业  3家电行业
    shopInfo(item) {
        // wx.navigateTo({
        //     url: `/packageC/pages/choiceMealPerson/index`,
        // })

        // wx.navigateTo({
        //     url: `/packageC/pages/choiceMealPerson/index?storeId=8637&TableId=7&deskName=22`,
        // })

        // return;
        
        var that = this
        var tempJson = {
            storeId: item.id,
            // storeId:33,
            xpoint: this.data.lat,
            ypoint: this.data.lon,
            xcFlag: '0' //是否含有携程门票 0不含有
        }
        common.get('/store/mainPageInfo', tempJson, res => {
            that.setData({
                industryType: res.data.store.industryType
            }, () => {
                if (this.data.cid == 60) {
                    wx.navigateTo({
                        url: `/packageA/pages/freshIndex/freshIndex?id=${item.id}&name=${item.storeName}`,
                    })
                    return
                }
                if (this.data.cid == 61) {
                    wx.navigateTo({
                        url: `/packageC/pages/householdElectrical/householdElectrical?id=${item.id}&name=${item.storeName}`,
                    })
                    return
                }
                if (this.data.industryType == 2) {
                    wx.navigateTo({
                        url: `/packageC/pages/activeBei/activeBei?id=${item.id}&name=${item.storeName}`,
                    })

                    return
                }

                if (item.storeType == 30) {  //酒店页面
                    var url = '/packageB/pages/hotel/hotel?id=' + item.id + '&name=' + item.storeName
                } else if (item.storeType == 29) { 
                       //餐饮
                    // var url = '/packageA/pages/activety/activety?id=' + item.id + '&name=' + item.storeName
                     var url = '/packageC/pages/fineFood/fineFood?id=' + item.id + '&name=' + item.storeName
                    
                }
                // 后加的
                else if (item.storeType == 5) {    //场馆
                    var url = '../../ScenicArea/myselfDetailPage/index?id=' + item.id + '&name=' + item.storeName + '&type=' + 'pe'
                } else if(item.storeType==18){
                    var url = '/packageC/pages/Craftsman/Craftsman?id=' + item.id + '&name=' + item.storeName + '&type=' + 'pe'
                } else {
                    var url = '/packageA/pages/Goods/storePage/index?id=' + item.id + '&name=' + item.storeName
                }
                wx.navigateTo({
                    url: url
                })
            })
            console.log(res.data.store, "75456666665")
        })
    },

    onLoad: function (options) {
        const that = this
        const name = options.name
        if (name) {
            wx.setNavigationBarTitle({
                title: name,
            })
        }

        console.log(options.id)
        that.setData({
            cid: options.id,
            imageUrl: that.selectImg(options.id),
            isLoadingBusy: true
        }, function () {
            this.getTopImage()
            getApp().getLocation(function (data) {
                that.data.lon = data.longitude
                that.data.lat = data.latitude
                that.data.city = data.regeocodeData.addressComponent.city
                that.getStoreList(true)

            }, function () {
                // 定位失败回调
                that.setData({
                    isLoadingBusy: false
                })
            })
        })
    },
    selectImg(id) {
        let imgUrl = ''
        switch (id) {
            case '1': // 
                imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E6%95%99%E8%82%B2%E6%B5%B7%E6%8A%A5.png'
                break;
            case '4':
                imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E7%8E%89%E5%99%A8.png'
                break;
            case '7':
                imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E8%AF%97%E9%85%92%E8%8A%B1%E8%8C%B6.png'
                break;
            case '5':
                imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E5%91%A8%E8%BE%B9%E6%B8%B8%E6%B5%B7%E6%8A%A5.png'
                break;
            default:
                imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E6%95%99%E8%82%B2%E6%B5%B7%E6%8A%A5.png'
                break;
        }
        return imgUrl
    },
    click_deitails() {

        if (this.data.industryType == 1) {
            var url = '/packageC/pages/activeBei/activeBei'
        }
        wx.navigateTo({
            url: url
        })
    },
    storeListClick(e) {
        const item = e.currentTarget.dataset.item
        this.shopInfo(item)

    },
    goodsImgClick(e) {
        const item = e.currentTarget.dataset.item
        common.gotoGoodsPage(item)
    },
    getStoreList(refresh = false) {
        if (!refresh && (this.data.isLoadingBusy || this.data.noMoreData)) {
            return;
        }
        this.setData({
            isLoadingBusy: true,
            refreshing: refresh
        })
        let that = this
        const pageNum = refresh ? 1 : this.data.pageNum + 1
        let tempJson = {
            storeTypeId: this.data.cid,
            pageNum,
            pageSize: 10,
            xpoint: that.data.lon,
            ypoint: that.data.lat,
            distance: '',
            recentIncome: '',
            commentRate: '',
            cityName: that.data.city
        }
        common.post('/store/getSearchStoreListByStoreTypeId', tempJson, function (res) {
            if (res.status == 200) {
                that.data.pageNum = pageNum
                that.setData({
                    storeList: refresh ? res.data : [...that.data.storeList, ...res.data]
                })
            } else {
                that.setData({
                    noMoreData: true
                })
            }
        }, function () {
            that.setData({
                isLoadingBusy: false,
                refreshing: false
            })
        })
    },
    onReachBottom() {
        this.getStoreList(false)
    }
})